package com.hanzhong.travel.service.impl;

import com.hanzhong.travel.entity.Food;
import com.hanzhong.travel.mapper.FoodMapper;
import com.hanzhong.travel.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 美食服务实现类
 */
@Service
public class FoodServiceImpl implements FoodService {

    @Autowired
    private FoodMapper foodMapper;

    @Override
    public boolean addFood(Food food) {
        return foodMapper.insert(food) > 0;
    }

    @Override
    public Food getFoodById(Long id) {
        return foodMapper.selectById(id);
    }

    @Override
    public List<Food> getAllFoods() {
        return foodMapper.selectAll();
    }

    @Override
    public List<Food> getFoodsByCategory(String category) {
        return foodMapper.selectByCategory(category);
    }

    @Override
    public Map<String, Object> getFoodsByPage(int page, int size) {
        // 计算分页偏移量
        int offset = (page - 1) * size;
        
        // 查询分页数据
        List<Food> foods = foodMapper.selectByPage(offset, size);
        
        // 查询总数
        int total = foodMapper.selectCount();
        
        // 计算总页数
        int totalPages = (int) Math.ceil((double) total / size);
        
        // 构建返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("content", foods);
        result.put("totalElements", total);
        result.put("totalPages", totalPages);
        result.put("size", size);
        result.put("number", page);
        result.put("hasNext", page < totalPages);
        result.put("hasPrevious", page > 1);
        
        return result;
    }

    @Override
    public boolean updateFood(Food food) {
        return foodMapper.update(food) > 0;
    }

    @Override
    public boolean deleteFood(Long id) {
        return foodMapper.deleteById(id) > 0;
    }
} 